package com.hava.demo.springmvc.onlyone.controller.web;

import com.google.common.collect.Maps;
import com.hava.demo.springmvc.onlyone.entity.OnlyOne;
import com.hava.demo.springmvc.onlyone.service.OnlyOneService;
import com.hava.web.serlvet.QueryRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by zhanpeng on 15-2-26.
 * 一般意义上的web只使用GET和POST的HTTP函数，而新的HTTP函数还具有其他，必要在其他的地方进行使用
 * OnlyOne管理的Controller, 使用Restful风格的Urls:
 *
 * List page : GET /onlyone/
 * Create page : GET /onlyone/create
 * Create action : POST /onlyone/create
 * Update page : GET /onlyone/update/{id}
 * Update action : POST /onlyone/update
 * Delete action : GET /onlyone/delete/{id}
 */

@Controller
//声明把OnlyOneController映射到/demo/onlyone
@RequestMapping(value = "/demo/onlyone")
public class OnlyOneWebController {
    
    //TODO:没有添加log
    
    @Autowired
    private OnlyOneService onlyOneService;
    
    //在所有函数运行之前都会执行的内容
    @ModelAttribute
    public void arunBefore()
    {
        System.out.println("This method will run other before.");
    }
    
    //只有update方法有对id属性的需求，因此仅仅在update执行时进行实际执行。
    @ModelAttribute
    public void getOnlyOne(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model)
    {
        System.out.println("This method check the entity id attributes.");
        if(id != -1)
        {
            model.addAttribute("onlyone",onlyOneService.getOnlyOne(id));
        }
    }
    
//    //一般页面请求
//    @RequestMapping(method = RequestMethod.GET)
//    public String list()
//    {
//
//        return null;
//    }
    //执行函数，基础的增删该查
    //创建或新增
    @RequestMapping(value = "create",method = RequestMethod.GET)
    public String createView(Model model)
    {
        model.addAttribute("onlyone",new OnlyOne());
        model.addAttribute("action","create");
        
        return "demo/onlyone/create";
    }
    
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(@Valid OnlyOne newOnlyOne, RedirectAttributes redirectAttributes)
    {
        onlyOneService.createOnlyOne(newOnlyOne);
        redirectAttributes.addFlashAttribute("message","Onlyne创建");
        return "redirect:/demo/onlyone/";
    }
    
    //删除
    @RequestMapping(value = "delete/{id}")
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes)
    {
        onlyOneService.deleteOnlyOne(id);
        return "redirect:/demo/onlyone/";
    }
    
    //修改
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, Model model)
    {
        model.addAttribute("onlyone",onlyOneService.getOnlyOne(id));
        model.addAttribute("action","update");
        
        return "demo/onlyone/update";
    }
    
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String update(@Valid OnlyOne updateOnlyOne,RedirectAttributes redirectAttributes)
    {
        onlyOneService.updateOnlyOne(updateOnlyOne);
        //TODO:没有实现i18n
        redirectAttributes.addFlashAttribute("message","Onlyone更新");
        
        return "redirect:/demo/onlyone/";
    }
    
    //查询
    private static final String PAGE_SIZE = "3";
    private static Map<String,String> sortTypes = Maps.newLinkedHashMap();
    static {
        //sortTypes.put("auto", "自动");
        //sortTypes.put("title", "标题");
    }
    
    @RequestMapping(method = RequestMethod.GET)
    public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
                       @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
                       @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model,
                       ServletRequest request) {

        //warning:search_ & reviews? is same thing
        Map<String, Object> searchParams = QueryRequestUtil.getParameter(request,"search_");
        //该demo为单一只有一个实体，不需要交叉查询与获取用户相关信息
        //Long userId = getCurrentUserId();
        
        System.out.println("page:" + pageNumber);
        System.out.println("page.size:" + pageSize);
        System.out.println("sortType:" + sortType);

        System.out.println("db begin");
        Page<OnlyOne> onlyOnes = onlyOneService.searchOnlyOnePage(searchParams, pageNumber, pageSize, sortType);

        System.out.println("db final");
        
        model.addAttribute("onlyones", onlyOnes);
        model.addAttribute("sortType", sortType);
        model.addAttribute("sortTypes", sortTypes);
        // 将搜索条件编码成字符串，用于排序，分页的URL
        model.addAttribute("searchParams", QueryRequestUtil.encodeParameter(searchParams, "search_"));

        return "demo/onlyone/list";
    }
}